<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>添加品牌</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
</head>
<body onload="getParams()">

<div id="app">

    <h3>添加品牌</h3>
    <form action="#" method="post">
        <input type="hidden" id="id" name="id" v-model="brand.id">
        品牌名称：<input id="brandName" name="brandName" v-model="brand.brandName"><br>
        企业名称：<input id="companyName" name="companyName" v-model="brand.companyName"><br>
        排序：<input id="ordered" name="ordered" v-model="brand.ordered"><br>
        描述信息：<textarea rows="5" cols="20" id="description" name="description"
                           v-model="brand.description"></textarea><br>
        状态：
        <input type="radio" name="status" value="0" v-if="brand.status==0" checked="checked">禁用
        <input type="radio" name="status" value="1" v-if="brand.status==1" checked="checked">启用<br>

        <input type="button" id="btn" value="提交" @click="requestAddOrUpdateBrand">
    </form>
</div>
<script type="text/javascript">

    var vm = new Vue({
        el: '#app',
        data: {
            brand: {}
        },
        methods: {
            getParams() {
                var queryString = window.location.search;
                if (queryString.length > 0) {
                    let urlParams = new URLSearchParams(queryString);
                    let id = urlParams.get("id")
                    axios({
                        url: '/brand-case-vue/brandSel?id=' + id,
                        method: 'GET'
                    }).then((response) => {
                        this.brand = response.data;
                    })

                }
            },
            requestAddOrUpdateBrand() {
                //1.获取表单数据
                var reqUrl = "/brand-case-vue/brandAdd";
                //隐藏id是否有值
                if (this.brand.id != "" && this.brand.id != null && this.brand.id != undefined) {
                    reqUrl = "/brand-case-vue/brandEdit";
                } else {

                    reqUrl = '/brand-case-vue/brandAdd';
                }
                //3.发送Ajax请求
                axios({
                    url: reqUrl,
                    data: this.brand,
                    method: 'POST'
                }).then((response) => {
                    //成功跳转列表页
                    if (response.data) {
                        window.location.href = "brand.html"
                    } else {
                        alert("操作失败");
                    }
                })
            }
        },
        mounted() {

            this.getParams()
        }
    })


</script>

</body>
</html>